﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Web.SystemLogin.Models;

namespace Web.SystemLogin.Business
{
    public class CheckOnline
    {
        const int DELAY_TIMES = 20000;    //定义执行的时间间隔为20秒
        const int DELAY_SECONDS = 300;     //将用户掉线时间设置为300秒

        private Thread thread;      //定义内部线程
        private static bool flag = false;   //定义唯一标志

        public CheckOnline()
        {
            if (!flag)
            {
                flag = true;
                this.thread = new Thread(new ThreadStart(ThreadProc));
                thread.Name = "online user";
                thread.Start();
            }
        }


        internal void ThreadProc()
        {
            while (true)
            {
                OnlineUser temp = new OnlineUser();  //定义一个用户对象
                //开始检查是否有用户过期了    
                string strExpr;
                //tmpuser.curtime.AddSeconds(DELAY_SECONDS).CompareTo(DateTime.Now)<0
                strExpr = "RefreshTime < '" + DateTime.Now.AddSeconds(0 - DELAY_SECONDS) + "'";

                OnlineUserDs.OnlineUserRow[] curUser;
                // Use the Select method to find all rows matching the filter.
                curUser = (OnlineUserDs.OnlineUserRow[])temp.AllUser.Select(strExpr);

                if (curUser.Length > 0)
                {
                    //删除这些记录
                    for (int i = 0; i < curUser.Length; i++)
                    {
                        curUser[i].Delete();
                    }
                    temp.AllUser.AcceptChanges();
                }
                Thread.Sleep(DELAY_TIMES);
            }
        }
    }
}
